<?php
    use User\Model\LpicModel;
?>
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                菜单详情
            </header>
            <div class="panel-body">
                <form class="form-horizontal" method="post"
                      action="/User/Lpic/menuUpdate">
                    <input type="hidden" value="<?php echo I('get.liveId', '', '');?>" name="live_id" id="live_id">
                    <input type="hidden" value="<?php $val = (!empty($menu)) ?
                            $menu['menu_id'] : ''; echo $val;?>" name="menuId" id="menuId">
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">菜单名称:</label>

                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="name" value="<?php $val=(!empty($menu)) ?
                            $menu['name'] : ''; echo $val;?>" id="name">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">菜单地址:</label>

                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="content" value="<?php $val=(!empty($menu)) ?
                            $menu['content'] : ''; echo $val;?>" id="content">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">菜单图标:</label>

                        <div class="col-sm-10">
                            <img src="<?php
                        if(empty($menu) || empty($menu['icon'])){
                            echo '';
                        }else{
                            echo $menu['icon'];
                        }
                            ?>" alt="" id="icon_url" style="width: 200px;height: 200px;">
                            <input name="icon" id="icon" type="file">
                        </div>
                    </div>


                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-10">
                            <button class="btn btn-primary" type="submit" id="submit">提交</button>
                            <button class="btn btn-default" type="button" onclick="history.back(-1);">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </section>
    </div>
</div>
<script>
    //--图片处理--
    var icon_data = '';
    $(function () {
        var head_img = document.getElementById("icon");
        head_img.addEventListener('change', readFile, false);
    });
    function readFile() {
        var file = this.files[0];
        if (!/image\/\w+/.test(file.type)) {
            alert("请确保文件为图像类型");
            return false;
        }
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
            $("#icon_url").attr("src", this.result);
            icon_data = this.result;
        }
    }


    $("#submit").click(function () {
        var data = $.param({"icon": icon_data}) + "&" + $("form").serialize();
        var url = $("form").attr("action");
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: data,
            url: url,
            async:false,
            success: function (res) {
                if (res.code == 1) {
                    alert("提交成功");
                    window.location = '/User/Lpic/menu?id=' + res.live_id;
                } else {
                    alert(res.msg);
                }
            }
        });
        return false;
    });
</script>